package TableEditions;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

import TableAdditions.main;
import Tables.SupplierTableInterface;
import Utility.JTextFieldFilter;
import Utility.Supplier;


public class SupplierTableEdition extends JFrame {
    
	
	
	
	public Supplier[] supplierArray;
	public Supplier supplierToAdd= new Supplier();
	boolean supplierCodeFlag=false;
	boolean supplierNameFlag=false;	
	Connection con = null;
    Statement st = null;
    ResultSet rs = null;

    String url = "jdbc:mysql://localhost:3306/mydb";
    String user = "root";
    String password = "qweasdzxc";
	
	
	SupplierTableInterface cti;
	private JTextField supplierCodeTextField;
	private JTextField supplierNameTextField;
	private JTextField supplierAddressTextField;
	private JTextField supplierPhoneTextField; 
	public SupplierTableEdition(final SupplierTableInterface supplierTableInterface,final Supplier supplier) {
		
		super("Tedarikçi Düzenleme Ekranı");
		this.cti = supplierTableInterface;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(600,200);
		getContentPane().setLayout(null);
		
		JLabel lblSupplierCode = new JLabel("Tedarikçi Kodu");
		lblSupplierCode.setBounds(5, 5, 124, 19);
		getContentPane().add(lblSupplierCode);
		
		supplierCodeTextField = new JTextField();
		supplierCodeTextField.setText(supplier.getSupplierName());
		supplierCodeTextField.setEditable(false);
		supplierCodeTextField.setBounds(124, 5, 124, 19);
		getContentPane().add(supplierCodeTextField);
		supplierCodeTextField.setColumns(10);
		
		JLabel lblSupplierName = new JLabel("Tedarikçi Adı");
		lblSupplierName.setBounds(284, 5, 124, 19);
		getContentPane().add(lblSupplierName);
		
		supplierNameTextField = new JTextField();
		supplierNameTextField.setText(supplier.getSupplierName());
		supplierNameTextField.setBounds(417, 5, 124, 19);
		getContentPane().add(supplierNameTextField);
		supplierNameTextField.setColumns(10);
		
		JLabel lblSupplierAddress = new JLabel("Tedarikçi Adresi");
		lblSupplierAddress.setBounds(5, 40, 124, 19);
		getContentPane().add(lblSupplierAddress);
		
		supplierAddressTextField = new JTextField();
		supplierAddressTextField.setText(supplier.getSupplierAddress());
		supplierAddressTextField.setBounds(124, 40, 124, 19);
		getContentPane().add(supplierAddressTextField);
		supplierAddressTextField.setColumns(10);
		
		JLabel lblSupplierPhone = new JLabel("Tedarikçi Telefonu");
		lblSupplierPhone.setBounds(284, 40, 142, 19);
		getContentPane().add(lblSupplierPhone);
		
		supplierPhoneTextField = new JTextField();
		supplierPhoneTextField.setText(supplier.getSupplierPhone());
		supplierPhoneTextField.setBounds(417,40, 124, 19);
		//supplierPhoneTextField.setDocument(new JTextFieldFilter(JTextFieldFilter.NUMERIC));
		getContentPane().add(supplierPhoneTextField);
		supplierPhoneTextField.setColumns(10);
		

		
		
		
		
		
		
		JButton editButton = new JButton("Düzenle");
		editButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
		
					supplierToAdd.setSupplierAddress(supplierAddressTextField.getText());
					supplierToAdd.setSupplierPhone(supplierPhoneTextField.getText());
					supplierToAdd.setSupplierName(supplierNameTextField.getText());
					/*System.out.println("Ready to add supplier");
					System.out.println("code= "+supplierToAdd.getSupplierCode());
					System.out.println("name= "+supplierToAdd.getSupplierName());
					System.out.println("address= "+supplierToAdd.getSupplierAddress());
					System.out.println("phone= "+supplierToAdd.getSupplierPhone());*/
					
					
					
					try {
						con = (Connection) DriverManager.getConnection(url, user, password);
				        st = (Statement) con.createStatement();
						st.executeUpdate("UPDATE supplier SET supplier_name='"+supplierToAdd.getSupplierName()+"'," +
								"supplier_address='"+supplierToAdd.getSupplierAddress()+"'," +
										"supplier_phone='"+supplierToAdd.getSupplierPhone()+"' " +
												"WHERE supplier_code='"+supplier.getSupplierCode()+"'" );
						int messageType = JOptionPane.PLAIN_MESSAGE;
						JOptionPane.showMessageDialog(null, "Güncelleme Yapıldı", "Uyarı!!", messageType);
						con.close();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
				
				  	cti.addNewItem();
			        setVisible(false);
			        dispose();
			}
			
		});
		editButton.setBounds(441, 71,120, 25);
		getContentPane().add(editButton);
		
		
	 	
        try {
            con = (Connection) DriverManager.getConnection(url, user, password);
            st = (Statement) con.createStatement();
            rs=st.executeQuery("SELECT * from supplier WHERE activity='1' order by supplier_name");
            int i=0,counter=0;
            while(rs.next())
            	++counter;
            if(counter>0){
            rs.first();
            supplierArray=new Supplier[counter];
            supplierArray[i]=new Supplier();
            supplierArray[i].setSupplierCode(rs.getString("supplier_code"));
            supplierArray[i].setSupplierName(rs.getString("supplier_name"));
            supplierArray[i].setSupplierAddress(rs.getString("supplier_address"));
            supplierArray[i].setSupplierPhone(rs.getString("supplier_phone"));
            i++;
            while(rs.next())
            {
            	 supplierArray[i]=new Supplier();
                 supplierArray[i].setSupplierCode(rs.getString("supplier_code"));
                 supplierArray[i].setSupplierName(rs.getString("supplier_name"));
                 supplierArray[i].setSupplierAddress(rs.getString("supplier_address"));
                 supplierArray[i].setSupplierPhone(rs.getString("supplier_phone"));
                 i++;
            }
            
            }
            
            
            
           con.close(); 
            
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(main.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(main.class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }
        
        
       
		setVisible(true);
		
		
	}
}
